package com.daocao.hua.api.requestInfo;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;


import com.daocao.hua.activity.Application;
import com.daocao.hua.activity.AutonymActivity;
import com.daocao.hua.activity.AutonymSuccessActivity;
import com.daocao.hua.activity.EmailIdentityActivity;
import com.daocao.hua.api.Api;
import com.daocao.hua.api.bean.AuthSkipBean;
import com.daocao.hua.api.bean.IndentityType;
import com.daocao.hua.api.bean.KeyValueBean;
import com.daocao.hua.api.bean.LiMuKeyBean;
import com.daocao.hua.api.bean.LiMuSignBean;
import com.daocao.hua.api.bean.LiMuTokenBean;
import com.daocao.hua.api.bean.ModifyPhoneCheckCodeBean;
import com.daocao.hua.api.bean.ModifyPhoneSendCodeBean;
import com.daocao.hua.api.bean.SubmitApplyBean;
import com.daocao.hua.api.bean.UploadImageBean;
import com.daocao.hua.api.callback.LoanForNewContract;
import com.daocao.hua.api.callback.OperatorContract;
import com.daocao.hua.constant.Const;
import com.daocao.hua.fragment.LoanForNewFragment;
import com.daocao.hua.model.NewBeginApplyStepModel;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.PreferenceUtils;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;

import java.util.ArrayList;

import kangcheng.com.lmzx_android_sdk_v10.bean.Fuction;
import kangcheng.com.lmzx_android_sdk_v10.bean.MessageEvent;
import kangcheng.com.lmzx_android_sdk_v10.commom.sdk.LmzxSDK;
import kangcheng.com.lmzx_android_sdk_v10.commom.sdk.LmzxSdkImpl;

/**
 * Created by Administrator on 2017/5/14.
 */

public class LoanForNewRequest extends BaseRequest<LoanForNewContract.View> implements LoanForNewContract.Presenter<LoanForNewContract.View> {

    public static LoanForNewRequest create() {
        return new LoanForNewRequest();
    }

    //      3：淘宝认证
    //                4：京东认证
//                5：领英认证
//                6：公积金认证
//                7：社保认证
//                8：通讯认证
    public final String TAOBAO_TYPE = "taobao";
    public final String JD_TYPE = "jd";
    public final String GONGJIFEN_TYPE = "housefund";
    public final String SHEBAO_TYPE = "socialsecurity";
    public final String YUNYINGSHANG_TYPE = "mobile";
    public final String LINGYING_TYPE = "linkedin";

    @Override
    public void updateFace(String base64) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        UploadImageBean bean = new UploadImageBean();
        bean.img = base64;
        Api.api_service.uploadImageFace(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.updateFaceSuccess(result);
                });
    }

    @Override
    public void getSubmitApply(String deal_id, String lat, String lng) {

        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        SubmitApplyBean bean = new SubmitApplyBean();
        bean.deal_id = deal_id;
        bean.lat = lat;
        bean.lng = lng;
        bean.use_type = PreferenceUtils.getString(BaseContext, "use_type", "");
        Api.api_service.getSubmitApplyInfo(bean).compose(RxLifeUtil.NocheckOnDestroy(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                        mView.showError();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getSubmitApplySuccess(result);
                });
    }

    @Override
    public void getKey(String deal_id) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        LiMuKeyBean bean = new LiMuKeyBean();
        bean.deal_id = deal_id;
        Api.api_service.getLiMuKey(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getKeySuccess(result);
                });
    }

    @Override
    public void getSign(String params) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        LiMuSignBean bean = new LiMuSignBean();
        bean.params = params;
        Api.api_service.getSign(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getSignSuccess(result);
                });
    }

    @Override
    public void uploadToken(String deal_id, String token, String bizType) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        LiMuTokenBean bean = new LiMuTokenBean();
        bean.deal_id = deal_id;
        bean.token = token;
        bean.biztype = bizType;
        Api.api_service.uploadToken(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.uploadTokenSuccess(result);
                });
    }

    @Override
    public void getModifyphoneSendcode(String mobile) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        ModifyPhoneSendCodeBean bean = new ModifyPhoneSendCodeBean();
        bean.mobile = mobile;
        Api.api_service.getModifyphoneSendcode(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getModifyphoneSendcode(result);
                });
    }

    @Override
    public void getModifyphoneCheckcode(String mobile, String verify_code) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        ModifyPhoneCheckCodeBean bean = new ModifyPhoneCheckCodeBean();
        bean.mobile = mobile;
        bean.verify_code = verify_code;
        Api.api_service.getModifyphoneCheckcode(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getModifyphoneCheckcode(result);
                });
    }

    @Override
    public void getAuthSkipSet(String deal_id, String code, String type) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        AuthSkipBean bean = new AuthSkipBean();
        bean.code = code;
        bean.deal_id = deal_id;
        bean.type = type;
        Api.api_service.getAuthSkipSet(bean).compose(RxLifeUtil.NocheckOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getAuthSkipSetSuccess(result);
                });
    }

    //    初始化立木
    public void initLiMu(String uid, String apikey) {
        Application.getInstance().LmsdkInit(apikey, uid);
        LmzxSDK sdk = LmzxSdkImpl.getInstance();
        sdk.registEvent(BaseContext);
        sdk.setTextStyle(BaseContext, 0xFF4a4a4a);
        sdk.setButtonStyle(BaseContext, 0xFF2d7bff);
        sdk.setPageBack(BaseContext, 0xFFf4f4f4);
        sdk.setBannerColor(BaseContext, 0xFF2d7bff);
        sdk.setBannerTextColor(BaseContext, 0xFFFFFFFF);
//        LmzxSDK sdk = LmzxSdkImpl.getInstance();
        //参数依次为apiKey,UID,结果通知地址。这三个都必须
//        http://192.168.1.1/callbackUrl.jsp
//        sdk.init(BaseContext,apiKey,uid,callBakUrl);
//
//        sdk.init(BaseContext, apikey, uid, Const.SERVER_API_URL + "/limu/callback");//apikey 必须的
////        Log.i("dasdaadsasda", "apikey" + apikey + "uid=" + uid);
//        //设置测试的环境,不设置的话默认生产环境。
//        if (Const.IS_DEBUG) {
//            sdk.setEnv(BaseContext, "https://t.limuzhengxin.cn:3443/api/gateway");
////            sdk.setEnv(BaseContext, "https://t.limuzhengxin.cn/api/gateway");
//        } else {
//            sdk.setEnv(BaseContext, "https://api.limuzhengxin.com/api/gateway");
//        }
////        sdk.setEnv(BaseContext, "https://api.limuzhengxin.com/api/gateway");
//        //事件注册
//        sdk.registEvent(BaseContext);
//        sdk.setTextStyle(BaseContext, 0xFF4a4a4a);
//        sdk.setButtonStyle(BaseContext, 0xFFe3483d);
//        sdk.setPageBack(BaseContext, 0xFFf4f4f4);
//        sdk.setBannerColor(BaseContext, 0xFFe3483d);
//        sdk.setBannerTextColor(BaseContext, 0xFFFFFFFF);
    }


    //设置类型
    public void setType(String type) {
        if (type.equals(TAOBAO_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionTaoBao, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(YUNYINGSHANG_TYPE)) {
            if (TextUtils.isEmpty(PreferenceUtils.getString(getContext(), "mobile", ""))) {
                LmzxSdkImpl.getInstance().initOperatorPreNo(BaseContext, "", true);
            } else {
                LmzxSdkImpl.getInstance().initOperatorPreNo(BaseContext, PreferenceUtils.getString(getContext(), "mobile", ""), false);
            }

            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionMobileCarrie, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(JD_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionJD, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(GONGJIFEN_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionHousingFund, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(LINGYING_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionLinkedIn, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(SHEBAO_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionSocialSecurity, 0 + "", Const.LIMU_CALLBACK);
        }
    }

    public void setPageAdapter(ViewPager pager, FragmentManager fragmentManager, ArrayList<NewBeginApplyStepModel> datas) {
        pager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                return new LoanForNewFragment().setTitle(position, datas.get(position).step_title, datas.get(position).step_description, datas.get(position).banner);
            }

            @Override
            public int getCount() {
                return datas.size();
            }
        });
    }

    public String getTips(int poi) {
        switch (poi) {
            //                1：芝麻认证
//                2：刷脸认证
//                3：淘宝认证
//                4：京东认证
//                5：领英认证
//                6：公积金认证
//                7：社保认证
//                8：通讯认证
            case 0:
                return "请先进行芝麻认证";
            case 1:
                return "请先进行刷脸认证";
            case 2:
                return "请先进行淘宝认证";
            case 3:
                return "请先进行京东认证";
            case 4:
                return "请先进行领英认证";
            case 5:
                return "请先进行公积金认证";
            case 6:
                return "请先进行社保认证";
            case 7:
                return "请先进行通讯认证";
            default:
                return "";
        }
    }
}
